﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Spire.Doc;
using Spire.Doc.Documents;

namespace EvTest.SpireWord
{
    public partial class ToPics : Form
    {

        private string saveDir = @"D:\EvTest\File\";

        public ToPics()
        {
            InitializeComponent();
        }


        //自定义文件夹
        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();

            if (fbd.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = fbd.SelectedPath;
                saveDir = fbd.SelectedPath;
            }
        }

        private void btn1_Click(object sender, EventArgs e)
        {
            OpenFileDialog odf = new OpenFileDialog();
            odf.Multiselect = true;
            odf.RestoreDirectory = true;
            odf.Title = "打开你所选取的文件";
            odf.Filter = "Word文档|*.doc;*.docx";

            if (odf.ShowDialog() == DialogResult.OK)
            {
                if (odf.FileNames.Length == 0)
                {
                    MessageBox.Show("您未选择任何文件");
                    return;
                }

                for (int i = 0; i < odf.FileNames.Length; i++)
                {
                    dgv.Rows.Add(odf.SafeFileNames[i], odf.FileNames[i]);
                }

            }
        }

        private void btn2_Click(object sender, EventArgs e)
        {
            if (dgv.Rows.Count == 0)
            {
                MessageBox.Show("您还没有选择文件");
                return;
            }

            string x = cb1.SelectedItem.ToString();

            foreach (DataGridViewRow R in dgv.Rows)
            {
                Document document = new Document();

                document.LoadFromFile(R.Cells[1].Value.ToString().Trim());

                //Save doc file.
                Image img = document.SaveToImages(0, ImageType.Metafile);
                img.Save(x == "PNG" ? (saveDir + "/" + R.Index + "sample.png") : (saveDir + "/" + R.Index + "sample.jpg"), x == "PNG" ? System.Drawing.Imaging.ImageFormat.Png : System.Drawing.Imaging.ImageFormat.Jpeg);

                //Launching the image file.
                textBox2.Text = R.Cells[0].Value.ToString() + "已转换;\r\n" + textBox2.Text + "\r\n";
            }

            System.Diagnostics.Process.Start("explorer.exe", saveDir);
        }
    }
}
